<?php

/**
 * Created by Atom.
 * User: qiliping
 * Date: 2017/1/11
 * Time: 16:16
 */
class Teachers_model extends CI_Model
{
  /**
   * 根据用户ID
   * 获取老师详情
   * @param  [type] $user_id [description]
   * @return [type]          [description]
   */
  public function get_teacher_by_user_id($user_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_teachers where user_id = '{$user_id}' LIMIT 1";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 根据ID获取老师详情
   * @param  [type] $teacher_id [description]
   * @return [type]             [description]
   */
  function get_teacher_by_id($teacher_id){
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_teachers where id = '{$teacher_id}' LIMIT 1";
    return $this->db->query($sql)->row_array();
  }

  /**
  * 获取学校老师
  */
  function get_teacher_by_school($limit = 0, $offset = 10,$school_id){
        $this->db->trans_start();

        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}o2o_teachers where school_id={$school_id} order by id DESC LIMIT $limit, $offset;";
        $result = $this->db->query($sql)->result_array();
        $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();

        $this->db->trans_complete();

        return array("total_count" => $row_num['row_num'], "result" => $result);
    }

    /**
     * 重置密码
     * @param  [type] $teacher_id [description]
     * @param  [type] $password   [description]
     * @return [type]             [description]
     */
    public function change_pwd($teacher_id,$password)
    {
         //查询teacher
        $t_sql = "SELECT * FROM {$this->db->dbprefix}o2o_teachers where id = '{$teacher_id}' LIMIT 1";
        $teacher = $this->db->query($t_sql)->row_array();
        if(empty($teacher)){
          return 404;//老师不存在
        }

        //用户不存在添加
        $md5_pass = md5($password);
        $rand_str = '';
        for($i=0; $i<10; $i++){
          $rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
        }

        $password_str = md5($md5_pass.$rand_str);
        //更新user表
       $user_new = array(
         'rand_str' => $rand_str,
          'password' => $password_str,
       );

       return $this->db->update('user',$user_new, array('id' => $teacher['user_id']));
    }

    /**
     * 调整评分
     * @param  [type] $teacher_id   [description]
     * @param  [type] $school_score [description]
     * @return [type]               [description]
     */
    public function change_score($teacher_id,$school_score)
    {
         //查询teacher
        $t_sql = "SELECT * FROM {$this->db->dbprefix}o2o_teachers where id = '{$teacher_id}' LIMIT 1";
        $teacher = $this->db->query($t_sql)->row_array();
        if(empty($teacher)){
          return 404;//老师不存在
        }

        $teacher_new = array(
          'school_score'=> $school_score,
        );

        return $this->db->update('o2o_teachers', $teacher_new, array('id' => $teacher_id));
    }

    /**
     * 删除老师
     * @param  [type] $teacher_id [description]
     * @return [type]             [description]
     */
    public function delete($teacher_id)
    {
      $this->db->trans_start();
      //查询teacher
      $t_sql = "SELECT * FROM {$this->db->dbprefix}o2o_teachers where id = '{$teacher_id}' LIMIT 1";
      $teacher = $this->db->query($t_sql)->row_array();
      if(empty($teacher)){
        return -1;//老师不存在
      }

      //删除老师表
      $this->db->delete('o2o_teachers', array('id' => $teacher_id));

      //更新user表
       $user_new = array(
         'stream_id' => '',
         'teacher_id' => 0,
         'verified_anchor' => 0,
         'is_teacher' => 0,
       );
       $this->db->update('user',$user_new, array('id' => $teacher['user_id']));

       $this->db->trans_complete();

      return 1;
    }

    /**
    * 获取学校老师
    */
    function get_teacher_all_by_school($school_id){
      $sql = "SELECT  * FROM {$this->db->dbprefix}o2o_teachers where school_id={$school_id} ";
      return $this->db->query($sql)->result_array();
    }

    /**
     * 编辑老师
     * @param  [type] $teacher_id [description]
     * @param  [type] $data       [description]
     * @return [type]             [description]
     */
    function edit_teacher($teacher_id,$data){
      $this->db->trans_start();

      //查询teacher
      $t_sql = "SELECT * FROM {$this->db->dbprefix}o2o_teachers where id = '{$teacher_id}' LIMIT 1";
      $teacher = $this->db->query($t_sql)->row_array();
      if(empty($teacher)){
        return -1;//老师不存在
      }


      $stream_id = "";

      if($data['allow_live']==1){
        //开通直播
        $stream_id = "liveid".$teacher['user_id'].$teacher['mobile'];
      }
      //添加teacher信息

      $teacher_new = array(
        'teacher_name' => $data['teacher_name'],
        'speciality' => $data['speciality'],
        'allow_live' => $data['allow_live'],
        'stream_id' => $stream_id,
        'school_score'=> $data['school_score'],
        'birthday'=> $data['birthday'],
        'intro' => $data['intro'],
        'sort' => 0,
        'status' => $data['status'],
     );
      if(!empty($data['header_img'])){
        $teacher_new['header_img'] = $data['header_img'];
      }
      if(isset($data['sex'])){
        $teacher_new['sex'] = $data['sex'];
      }

     $this->db->update('o2o_teachers', $teacher_new, array('id' => $teacher_id));


     //更新user表
     $user_new = array(
       'stream_id' => $stream_id,
     );
     $this->db->update('user',$user_new, array('id' => $teacher['user_id']));
     $this->db->trans_complete();

     return 1;
    }

  /**
  * 添加老师
  */
  function add_teacher($school_id,$data){
      $this->db->trans_start();

      $user_id = 0;

      //查询user
      $user_sql = "SELECT * FROM {$this->db->dbprefix}user WHERE mobile='{$data['mobile']}' limit 1";
      $user_info = $this->db->query($user_sql)->row_array();
      if(empty($user_info)){
        //用户不存在添加
        $md5_pass = md5($data['password']);
        $rand_str = '';
        for($i=0; $i<10; $i++){
          $rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
        }

        $password = md5($md5_pass.$rand_str);

          $user_data = array(
          'login_name' => '',
          'rand_str' => $rand_str,
          'password' => $password,
          'mobile' => $data['mobile'],
          'is_reg_xmpp' => 0,
          'sex' => $data['sex'],
          'register_ip' => '',
          'status' => 1,
          'is_teacher' => 1,
          'add_time' => time(),
          'edit_time' => 0,
        );
        $this->db->insert('user', $user_data);
        $user_id = $this->db->insert_id();

        //user_account
        $user_account = array(
          'user_id' => $user_id,
          'balance' => 0,
          'integral' => 0,
          'commission_total' => 0
        );
        $this->db->insert('user_account', $user_account);
      }else if($user_info['is_teacher']==1) return -1;//该用户已经是老师，不能重复添加

      $stream_id = "";

      if($data['allow_live']==1){
        //开通直播
        $stream_id = "liveid".$user_id.$data['mobile'];
      }
      //添加teacher信息

      $teacher = array(
        'school_id' => $school_id,
        'user_id' => $user_id,
        'teacher_name' => $data['teacher_name'],
        'mobile' => $data['mobile'],
        'speciality' => $data['speciality'],
        'allow_live' => $data['allow_live'],
        'stream_id' => $stream_id,
        'comment_score'=> 100,
        'school_score'=> $data['school_score'],
        'birthday'=> $data['birthday'],
        'intro' => $data['intro'],
        'sort' => 0,
        'is_manager' => 0,
        'status' => $data['status'],
        'add_time' => time(),
     );
      if(!empty($data['header_img'])){
        $teacher['header_img'] = $data['header_img'];
      }
      if(isset($data['sex'])){
        $teacher['sex'] = $data['sex'];
      }
     $this->db->insert('o2o_teachers', $teacher);
     $teacher_id = $this->db->insert_id();

     //更新user表
     $user_new = array(
       'is_teacher' => 1,
       'teacher_id' => $teacher_id,
       'verified_anchor' => 1,
       'stream_id' => $stream_id,
     );
     $this->db->update('user',$user_new, array('id' => $user_id));

     $this->db->trans_complete();

     return $teacher_id;
  }

  function update_info_by_self($teacher_id,$data){
    $teacher = array(
      'teacher_name' => $data['teacher_name'],
      'mobile' => $data['mobile'],
      'header_img' => $data['school_logo'],
      'birthday' => $data['birthday'],
      'sex' => $data['sex'],
      'speciality' => $data['speciality'],
      'content' => $data['content'],
    );
  $this->db->update('o2o_teachers',$teacher,array('id' => $teacher_id ));
  }

  function get_teacher_score_list($teacher_id, $limit = 0, $offset = 10){
    $sql = "SELECT SQL_CALC_FOUND_ROWS ss.*,s.name,c.class_name FROM {$this->db->dbprefix}o2o_school_student_scores AS ss ";
    $sql .="LEFT JOIN {$this->db->dbprefix}o2o_student AS s ON(s.student_id = ss.student_id) ";
    $sql .="LEFT JOIN {$this->db->dbprefix}o2o_class AS c ON(c.id = ss.class_id) ";
    $sql .="WHERE ss.teacher_id = '{$teacher_id}' AND type = 2 LIMIT $limit, $offset ";
    $result = $this->db->query($sql)->result_array();
    $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
    return array("total_count" => $row_num['row_num'], "result" => $result);
  }
}
